package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.model.Label;
import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.service.LabelService;
import com.bojiu.lawsearch.lawsearch.util.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: afdiao
 * @Date：2020/3/8 17:21
 * @Description: 官方标签管理
 */
@Api(description = "后台官方标签管理接口，功能已完成！！！")
@RestController
@RequestMapping("label")
public class BackLabelController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    LabelService labelService;

    /**
     * 后台获取官方标签列表
     *
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @ApiOperation(value = "标签列表", notes = "获取官方标签列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Label.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("back/list/{pageNum}/{pageSize}")
    public JsonResult getBackList(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        Page<Label> backLabelList = labelService.getBackLabelList(pageNum, pageSize);
        return new JsonResult(ResultCode.SUCCESS, backLabelList);
    }

    /**
     * 后台增加标签
     *
     * @param label
     * @param errors
     * @param request
     * @return
     */
    @ApiOperation(value = "添加标签", notes = "后台增加新的标签")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口"),
            @ApiResponse(code = 1200, message = "请求字段为空"),
            @ApiResponse(code = 1800, message = "标签名已存在")
    })
    @PostMapping("back/insert")
    public JsonResult backInsert(@ApiParam(name = "标签对象", value = "json格式", required = true) @Valid @RequestBody Label label, Errors errors, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }

        if (errors.hasErrors()) {
            JsonResult jsonResult = UtilTools.handleNullField(errors);
            return jsonResult;
        }

        JsonResult jsonResult = labelService.backInsert(label);
        return jsonResult;
    }

    /**
     * 后台更新标签
     *
     * @param id
     * @param label
     * @param errors
     * @param request
     * @return
     */
    @ApiOperation(value = "更新标签", notes = "后台更新标签")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口"),
            @ApiResponse(code = 1200, message = "请求字段为空"),
            @ApiResponse(code = 1800, message = "标签名已存在")
    })
    @PostMapping("back/update/{id}")
    public JsonResult backUpdate(@ApiParam(name = "id", value = "需要更新的标签id", required = true) @PathVariable("id") int id, @ApiParam(name = "标签对象", value = "json格式", required = true) @Valid @RequestBody Label label, Errors errors, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }

        if (errors.hasErrors()) {
            JsonResult jsonResult = UtilTools.handleNullField(errors);
            return jsonResult;
        }

        JsonResult jsonResult = labelService.backUpdate(label, id);
        return jsonResult;
    }

    /**
     * 后台官方标签删除
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "删除标签", notes = "后台删除标签")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("back/delete/{id}")
    public JsonResult backDelete(@ApiParam(name = "id", value = "需要删除的标签id", required = true) @PathVariable("id") int id, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        labelService.backDelete(id);
        return new JsonResult(ResultCode.SUCCESS);
    }
}
